package sg.nus.iss.vmcs.gui.elements;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionListener;

import sg.nus.iss.vmcs.VMCSException;

/**
 * Used to view the contents of a store, used in the Machinery Simulator Panel<br><br>
 * Modifications:
 * <li>27/12/2010 - Philip - Tidied up class and removed global imports</li>
 * <li>3/12/2010 - Philip - Converted this to a builder product, used for all StoreItem displays
 * <br><br>
 * @author Olivo Miotto
 * @author Philip
 */
public class StoreViewer extends Panel {

    /** Generated Serial ID  */
	private static final long serialVersionUID = -8734572341147320747L;
	
	/** Banner for the StoreViewer  */
	private Label lblBanner;
	
	/** Panel to Manage the View Items */
	private Panel pnlViewItems;
	
	/** List of Items to be viewed */
	private StoreViewerItem viewItems[];
	
	/** The currently active item */
	private StoreViewerItem activeItem;
	
	/**
	 * Returns an instance of the store viewer
	 */
	public StoreViewer() {
		this.setLayout(new BorderLayout());
	}
	
	/**
	 * Sets the Banner for the Store Viewer
	 * @param lblBanner
	 */
	public void setBanner(Label lblBanner) {
		if(lblBanner != null) this.remove(lblBanner);
		
		this.lblBanner = lblBanner;
		this.add(this.lblBanner, BorderLayout.NORTH);
	}
	
	/**
	 * Sets the Labelled Display for the product
	 * @param viewItems
	 */
	public void setStoreViewerItems(StoreViewerItem[] viewItems) {
		if(pnlViewItems != null) this.remove(pnlViewItems);
		
		Panel pnlViewPanel = new Panel();
		pnlViewPanel.setLayout(new GridLayout(viewItems.length, 1));
		this.viewItems = viewItems;
		
		for(StoreViewerItem ld : this.viewItems) {
			pnlViewPanel.add(ld);
		}
		
		this.add(pnlViewPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Sets the listener used for the components in the display
	 * @param listener
	 */
	public void setListener(ActionListener listener) {
		for(StoreViewerItem ld : viewItems) {
			ld.addListener(listener);
		}
	}
	
	/**
	 * Update quantity method for Store Viewer items
	 * @param idx Index to update
	 * @param qty New Quantity of Items
	 * @throws VMCSException Thrown if index exceeds number of items
	 */
    public void updateQty(int idx, int qty) throws VMCSException {
    	if(idx >= viewItems.length) {
    		viewItems[idx].setValue(qty);
    	}
    	else {
    		throw new VMCSException ("StoreViewer.update", "index overflow");
    	}
    }
    
	/**
	 * Update value method for Store Viewer items
	 * @param idx Index to update
	 * @param val New Value of Items
	 * @throws VMCSException Thrown if index exceeds number of items
	 */
    public void updateVal(int idx, int val) throws VMCSException {
    	if(idx >= viewItems.length) {
    		viewItems[idx].setValue(val);
    	}
    	else {
    		throw new VMCSException ("StoreViewer.update", "index overflow");
    	}
    }

	/**
	 * Update name method for Store Viewer items
	 * @param idx Index to update
	 * @param val New Value of Items
	 * @throws VMCSException Thrown if index exceeds number of items
	 */
    public void updateName(int idx, String name) throws VMCSException {
    	if(idx >= viewItems.length) {
    		viewItems[idx].setName(name);
    	}
    	else {
    		throw new VMCSException ("StoreViewer.update", "index overflow");
    	}
    }
    
    /**
     * Sets the Active or Inactive state of the Store Viewer
     * @param state True to set active, false if otherwise
     */
    public void setActive(boolean state) {
		for(StoreViewerItem ld : viewItems) {
			ld.setActive(state);
		}
    }
    
    /**
     * Gets a specific view item
     * @param idx Index of view item
     */
    public StoreViewerItem getViewItems(int idx) {
    	return viewItems[idx];
    }
    
    /**
     * Gets the number of view items in the store
     * @return
     */
    public int getViewItemCount() {
    	return viewItems.length;
    }
    
    /**
     * Sets the currently active store viewer item
     */
    public void setActiveItem(StoreViewerItem svi) {
    	activeItem = svi;
    }
    
    /**
     * Gets the currently active store viewer item
     */
    public StoreViewerItem getActiveItem() {
    	return activeItem;
    }
}